package com.yihaodian.index.meta;

import org.apache.log4j.Logger;

import com.google.inject.Inject;
import com.yihaodian.cluster.ClusterService;
import com.yihaodian.cluster.ClusterStateUpdate;

/**
 * Services for meta data of index.
 * 
 * Meta data of index including: - index - shards - schema
 * 
 * @author zhouhang
 * 
 */
public class DefaultIndexMetaService implements IndexMetaService {

	private static final Logger log = Logger
			.getLogger(DefaultIndexMetaService.class);

	ClusterService clusterService = null;

	@Inject
	public DefaultIndexMetaService(ClusterService clusterService) {
		this.clusterService = clusterService;
	}

	// private void init() {
	// if (init) {
	// return;
	// }
	//
	// log.info("init meta service ");
	// // Build index <-> cluster map
	// String indexId = DEFAULT_INDEX;
	// String clusterId = ZkClusterService.DEFAULT_CLUSTER;
	//
	// String[] indexes = { indexId };
	// String[] clusters = { clusterId };
	// _indexMap.putAll(indexes[0], Arrays.asList(clusters));
	// _clusterMap.putAll(clusters[0], Arrays.asList(indexes));
	//
	// // Init nodes
	// String[] nodeIds = { "127.0.0.1:8080", "127.0.0.1:8081" };
	// List<Node> nodes = new ArrayList<Node>();
	// for (String nid : nodeIds) {
	// Node node = new Node(nid);
	// nodes.add(node);
	// }
	//
	// IndexMeta meta = new IndexMeta(indexId);
	//
	// // Init Shard
	// String[] shardIds = { "shard1", "shard2" };
	// List<Shard> shards = new ArrayList<Shard>();
	// for (String sid : shardIds) {
	// Shard shard = new Shard(indexId, sid);
	// shards.add(shard);
	// }
	// meta.setShards(shards);
	//
	// // Init schema
	// String schemaFile = DefaultIndexMetaService.class.getClassLoader()
	// .getResource(DEFAULT_SCHEMA_NAME).getFile();
	// try {
	// String xml = FileUtils.readFileToString(new File(schemaFile));
	// IndexSchemaParser parser = new IndexSchemaParser();
	// IndexSchema schema = parser.fromXml(xml);
	// meta.setSchema(schema);
	// } catch (IOException e) {
	// log.error("Failed to read schem from " + schemaFile);
	// }
	//
	// // Build routing table
	// RoutingTable routing = new RoutingTable(indexId, clusterId);
	// for (int i = 0; i < nodes.size(); ++i) {
	// routing.addEntry(nodes.get(i).getId(), shards.get(i));
	// }
	//
	// // Build deploy map
	// Map<String, RoutingTable> indexRouting = new HashMap<String,
	// RoutingTable>();
	// indexRouting.put(clusterId, routing);
	// }

	@Override
	public void setIndexMeta(IndexMeta meta) {
		// TODO Auto-generated method stub
		ClusterStateUpdate update = null;
		clusterService.updateClusterState(update);

	}
}
